package com.leyuangame.core.controller.api.user;

import com.leyuangame.core.controller.bean.ResponseHandler.LeYuanResult;
import com.leyuangame.core.model.Game;
import com.leyuangame.core.model.GamePartion;
import com.leyuangame.dao.repository.jpa.GamePartionRepository;
import com.leyuangame.dao.repository.jpa.GameRepository;
import com.leyuangame.utils.ExceptionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class GameController {
    @Autowired
    private GameRepository gameRepository;

    @Autowired
    private GamePartionRepository gamePartionRepository;

    @PostMapping(value = "/rest/game/getOpenGames",produces = "application/json")
    @ResponseBody
    public LeYuanResult getOpenGames(){
        List<Game> games = gameRepository.findGamesByIsopenOrderBySort(0);
        HashMap<Object, Object> res = new HashMap<>();
        res.put("games",games);
        return LeYuanResult.ok(res);
    }

    @PostMapping(value = "/rest/game/getPartionsByGames",produces = "application/json")
    @ResponseBody
    public LeYuanResult getPartionsByGames(String gameid){
        try {
            Map res = new HashMap<>();
            List<GamePartion> partions = gamePartionRepository.findGamePartionsByGameidOrderBySort(gameid);
            res.put("partions",partions);
            return LeYuanResult.ok(res);
        }catch (Exception e){
            e.printStackTrace();
            return LeYuanResult.build(1,ExceptionUtil.getStackTrace(e));
        }
    }
}
